import  {setaddrede,getLisets,updatecart} from "../lib/lisets.js"
export default{
    state:{
       addres_Id:null,
	   cartList:[]
	   },
    mutations: {
       // 保存地址
	   mutationsAddresId(state,_id){
		   state.addres_Id=_id;
		   console.log("个发的广泛地规范的"+_id)
	   },
	   //购物车初始化
	   mutationsAddCartinit(state){
		   getLisets().then(res=>{
			   if(res.msg==='ok'){
				     state.cartList = res.data;
					  console.log(res)
			   }
			  
		   })
	   },
	   //加入购物车
	   
	   AddCart(state, options) {
			let {
				shop_id,
				num
			} = options;
			return setaddrede({
				shop_id: shop_id,
				skus_type: 0,
				num: num
			}).then(res => {
				console.log(res)
				return res
			})
		},
	   // 数量减少
	   		mutationsDelCart(state, index) {
	   			console.log(index)
	   			let num = state.cartList[index].num;
	   			if (num <=state.cartList[index].minnum) {
	   				Notify({
	   					type: 'warning',
	   					message: '不能在少了,比'+state.cartList[index].minnum+'都少了'
	   				});
	   				return false
	   			}else{
	   				updatecart({
	   					id: state.cartList[index].id,
	   					num:state.cartList[index].num--,
	   					
	   				}).then(res => {
	   					console.log(res)
	   				})
	   			}
	   			
	   		},
	   		//数量增加
	   		mutationsAddCart(state, index) {
	   			let num = state.cartList[index].num;
	   			if (num >state.cartList[index].maxnum) {
	   				Notify({
	   					type: 'warning',
	   					message: '不能在多了,比'+state.cartList[index].maxnum+'库存还多'
	   				});
	   				return false
	   			}else{
	   				updatecart({
	   					id: state.cartList[index].id,
	   					num:state.cartList[index].num++
	   				}).then(res => {
	   					console.log(res)
	   				})
	   			}
	   			
	   		},
	   		//选中未选中
	   		mutationsChangeCart(state,index){
	   			state.cartList[index].checked = !state.cartList[index].checked;
	   		},
			//反选
			mutationsnocheckCart(state,checkAll){
				console.log(checkAll)
				for(let i=0;i<state.cartList.length;i++){
					state.cartList[i].checked=checkAll
				}
			}
	   	},
	   	getters: {
	   		gettersTotal(state){
	   			let checkedList =state.cartList.filter((item,index)=>{
	   				return item.checked
	   			});
	   			let total=0;
	   			for(let i=0;i<checkedList.length;i++){
	   				total+=checkedList[i].pprice*checkedList[i].num
	   			};
	   			return total
				
	   		}
	   	}
}